package com.example.sample.activity;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.app.BaseActivity;
import com.example.sample.R;

import net.liteheaven.xblib.common.res.RawUtil;
import net.liteheaven.xblib.common.convert.StringUtil;
import net.liteheaven.xblib.widget.Toast.SmartToast;
import net.liteheaven.xblib.widget.Toast.ToastFactory;

import java.util.Timer;
import java.util.TimerTask;

public class LoadingPopupActivity extends BaseActivity {
	
	private static final String TAG = "LoadingPopupActivity";
	private Toast mToast;
    private LinearLayout mContainer;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_loading_popup);
        mContainer = (LinearLayout)findViewById(R.id.contain);
		findViewById(R.id.showBtn).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				final PopupWindow pw = showPopup(LoadingPopupActivity.this);
				new Handler().postDelayed(new Runnable() {
					
					@Override
					public void run() {
						pw.dismiss();
					}
				}, 2000);
			}
		});
        initButtons();
		
		testTimer();

		Activity a = LoadingPopupActivity.this;
		String rawStr = RawUtil.loadRawString(a, R.raw.info);
		ToastFactory.makeText(a, rawStr, 2000, 1).show();
	}
	
	private PopupWindow showPopup(Activity ctx){
		RelativeLayout view = (RelativeLayout) LayoutInflater.from(ctx).inflate(R.layout.popup_loading, null);
		//view.measure(0, 0);
		ImageView mImageView = (ImageView) view.findViewById(R.id.loadingIcon);
		TextView mTextView = (TextView) view.findViewById(R.id.loadingText);
		mTextView.setText("加载中...");
		Animation anim = AnimationUtils.loadAnimation(this, R.anim.rotate);
		mImageView.startAnimation(anim);
		PopupWindow pw = new PopupWindow(view, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		
		// 如果不响应返回键，需要屏蔽掉这句
		pw.setBackgroundDrawable(new BitmapDrawable());
		pw.setOutsideTouchable(false);
		pw.setFocusable(true);
		pw.showAtLocation(ctx.getWindow().getDecorView(),
				Gravity.CENTER | Gravity.CENTER, 0, 0);
		pw.update();
		return pw;
	}
	
	private void testTimer(){
		// 结论：艹，java 的timer不是在主线程
		Timer t = new Timer();
		Log.i(TAG, "testTimer main thread id = "  + Thread.currentThread().getId());
		
		t.schedule(new TimerTask() {
			
			@Override
			public void run() {
				Log.i(TAG, "testTimer timer thread id = "  + Thread.currentThread().getId());
			}
		}, 1000);
	}

    private void initButtons(){
        for (int i = 0; i< SmartToast.INTERPOLATOR_LIST.length; ++i){
            final String buttonName = SmartToast.INTERPOLATOR_LIST[i].getName();
            Button btn = new Button(this);
            final String forShort = StringUtil.getLastPart(buttonName, ".");
            btn.setText(forShort);
            mContainer.addView(btn);
            final int fi = i;
            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    SmartToast.cancelLastToast();
                    SmartToast st = ToastFactory.makeText(getApplicationContext(), forShort, 2000, 1);
                    st.setInterpolatorIndex(fi);
                    st.show();
                }
            });
        }
    }
}
